package io.renren.modules.seller.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.seller.dao.SpecificationOptionDao;
import io.renren.modules.seller.dao.TypeTemplateDao;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.SpecificationOptionEntity;
import io.renren.modules.seller.entity.TypeTemplateEntity;
import io.renren.modules.seller.service.TypeTemplateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service("typeTemplateService")
public class TypeTemplateServiceImpl extends ServiceImpl<TypeTemplateDao, TypeTemplateEntity> implements TypeTemplateService {

    @Autowired
    private SpecificationOptionDao specificationOptionDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TypeTemplateEntity> page = this.page(
                new Query<TypeTemplateEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageResult findPage(Map<String, Object> params) {
        String name = (String) params.get("name");
        IPage<TypeTemplateEntity> page = this.page(
                new Query<TypeTemplateEntity>().getPage(params),
                new QueryWrapper<TypeTemplateEntity>().
                        eq(StringUtils.isNotBlank(name), "name", name)
        );
        return new PageResult(page.getTotal(), page.getRecords());
    }

    @Override
    public List<Map> findSpecList(Long id) {
        //查询模板
        TypeTemplateEntity typeTemplate = this.getById(id);
        List<Map> list = JSON.parseArray(typeTemplate.getSpecIds(), Map.class);
        for (Map map : list) {
            //查询规格选项列表
            Long id1 = (Long) map.get("id");
            List<SpecificationOptionEntity> options = specificationOptionDao.selectSpecOptionsBySpecId(id1);
            map.put("options", options);
        }
        return list;
    }

}
